/*
 * Copyright [2012] Gurkan Erdogdu
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.   
*/    
package com.google.code.miniwebserver;

import java.util.concurrent.CountDownLatch;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

/**
 * Starting point of server kick up.
 * @author gurkanerdogdu
 *
 */
public class Main {
    
    /**Log*/
    private static final Log log = LogFactory.getLog(Main.class);
    
    /**For being used to run server infinitely until Ctrl-C is clicked*/
    private static CountDownLatch latch = new CountDownLatch(1);
    
    /**
     * Bootstrap of the mini server.
     * <p>
     * As an example I used HTTP Protocol Server with normal
     * sockets.
     * </p>
     * 
     * @param args program arguments
     * @throws Exception if any exception occurs
     */
    public static void main(String[] args) throws Exception{
        Server server = new Server(Constants.HTTP_11_PROTOCOL_HANDLER,8080);       
        
        //Init Server
        server.init();
        
        //Start Server
        server.start();
        
        //Add shuıtdown hook
        Runtime.getRuntime().addShutdownHook(new Thread(){           
            public void run(){
                log.info("Starting to shutdown server");
                latch.countDown();
            }            
        });        
        
        //Cause to wait here....  
        latch.await();
        log.info("Server has been shutdown");
               
    }
}
